package com.apex.lottery.controller;

import com.apex.lottery.common.result.CommonResult;
import com.apex.lottery.common.utils.JacksonUtil;
import com.apex.lottery.controller.param.DrawPrizeParam;
import com.apex.lottery.controller.param.ShowWinningRecordsParam;
import com.apex.lottery.controller.result.WinningRecordResult;
import com.apex.lottery.service.DrawPrizeService;
import com.apex.lottery.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.apex.lottery.controller.converter.DrawPrizeConverter.convertToWinningRecordResultList;

/**
 * @ClassName DrawPrizeController
 * @Description
 * @Author ZJX
 * @Date 2025/4/20 18:58
 * @Version 1.0
 **/
@RestController
public class DrawPrizeController {
    @Autowired
    private DrawPrizeService drawPrizeService;

    private static final Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);

    @PostMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Validated @RequestBody DrawPrizeParam param) {
        logger.info("drawPrize DrawPrizeParam:{}", param);
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    @PostMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(@Validated @RequestBody ShowWinningRecordsParam param) {
        logger.info("showWinningRecords ShowWinningRecordsParam:{}",
                JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(
                convertToWinningRecordResultList(winningRecordDTOList));
    }
}
